const terminal = {
  'windows nt 10': 'Windows 10',
  'windows nt 6.3': 'Windows 8.1',
  'windows nt 6.2': 'Windows 8',
  'windows nt 6.1': 'Windows 7',
  'windows nt 6.0': 'Windows Vista',
  'windows nt 5.2': 'Windows Server 2003XP x64',
  'windows nt 5.1': 'Windows XP',
  'windows xp': 'Windows XP',
  'windows nt 5.0': 'Windows 2000',
  'windows me': 'Windows ME',
  'win98': 'Windows 98',
  'win95': 'Windows 95',
  'win16': 'Windows 3.11',
  'macintosh|mac os x': 'Mac OS X',
  'mac_powerpc': 'Mac OS 9',
  'linux': 'Linux',
  'ubuntu': 'Ubuntu',
  'phone': 'iPhone',
  'pod': 'iPod',
  'pad': 'iPad',
  'android': 'Android',
  'blackberry': 'BlackBerry',
  'webos': 'Mobile',
  'freebsd': 'FreeBSD',
  'sunos': 'Solaris'
}

export const judgeTerminalBrowser = (userAgent) => {
  let data = {
    terminal: undefined,
    browser: undefined
  }
  let regs = {}
  const lowerUserAgent = userAgent.toLowerCase()
  for (let key in terminal) {
    if (new RegExp(key).test(lowerUserAgent)) {
      data.terminal = terminal[key]
      break
    }
  }

  // eslint-disable-next-line no-cond-assign
  if (regs = userAgent.match(/MSIE\s(\d+)\..*/)) {
    // ie 除11
    data.browser = 'ie ' + regs['1']
    // eslint-disable-next-line no-cond-assign
  } else if (regs = userAgent.match(/FireFox\/(\d+)\..*/)) {
    data.browser = 'firefox ' + regs['1']
    // eslint-disable-next-line no-cond-assign
  } else if (regs = userAgent.match(/Opera[\s|/](\d+)\..*/)) {
    data.browser = 'opera ' + regs['1']
    // eslint-disable-next-line no-cond-assign
  } else if (regs = userAgent.match(/Chrome\/(\d+)\..*/)) {
    data.browser = 'chrome ' + regs['1']
    // eslint-disable-next-line no-cond-assign
  } else if (regs = userAgent.match(/Safari\/(\d+)\..*$/)) {
    data.browser = 'safari ' + regs['1']
    // eslint-disable-next-line no-cond-assign
  } else if (regs = userAgent.match(/rv:(\d+)\..*/)) {
    data.browser = 'ie ' + regs['1']
  }

  return data
}
